package com.ytjj.qmyx.supplychain.common.utils;

/**
 * @Description TODO
 * @ClassName DateUtil
 * 
 * 
 * @Version 1.0
 **/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }

    /**
     * 日期转字符串时间
     * @return
     */
    public static String dateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }
    /**
     * 日期格式字符串转换成时间戳
     * @param date_str 字符串日期
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str,String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime()/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 取得当前时间戳（精确到秒）
     * @return
     */
    public static String timeStamp(){
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);
        return t;
    }

    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }
    /**
     *字符串的日期格式的计算
     */
    public static int daysBetween(String smdate,String bdate) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 计算两个日期之间相差的小时数
     */
    public static int hoursBetween(Date before, Date after) {
        long before_time = before.getTime();
        long after_time = after.getTime();
        long betweens = after_time - before_time;
        long between_hours = betweens / 3600 / 1000;
        return Integer.parseInt(String.valueOf(between_hours));
    }

    /**
     * 计算两个日期之间相差的秒数
     */
    public static long secondBetween(Date bigDate, Date smallDate) {
        long big_time = bigDate.getTime();
        long small_time = smallDate.getTime();
        long betweens = big_time - small_time;
        long between_second = betweens / 1000;
        return Long.parseLong(String.valueOf(between_second));
    }



    /**
     * 字符串转时间
     * @param str
     * @return
     */
    public static Date StrToTime(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 比较时间大小
     * @param date1
     * @param date2
     * @return
     */
    public static Boolean compareTime(String date1,String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Boolean result = false ;
        try{
            Date time1 = format.parse(date1);
            Date time2 = format.parse(date2);
            return time1.before(time2);
        }  catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static Boolean compareTime2(String date1,Date date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Boolean result = false ;
        try{
            Date time1 = format.parse(date1);
            return time1.before(date2);
        }  catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
    public static Boolean compareTime3(Date date2,String date1) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Boolean result = false ;
        try{
            Date time1 = format.parse(date1);
            return date2.before(time1);
        }  catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    //测试
    public static void main(String[] args) {
        System.out.println(DateUtil.compareTime("2024-06-12 13:51:47","2024-06-01 00:00:00"));

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            Date smallDate = format.parse("2023-03-17 10:29:00");
////            Date bigDate = format.parse("2023-01-01 10:12:10");
//            long l = secondBetween(new Date(), smallDate);
//            System.out.println("---->"+l);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }


//        String timeStamp = timeStamp();
//        System.out.println("timeStamp="+timeStamp); //运行输出:timeStamp=1553514820
//        System.out.println(System.currentTimeMillis());//运行输出:1553514820
//        //该方法的作用是返回当前的计算机时间，时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数
//
//        String date = timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss");
//        System.out.println("date="+date);//运行输出:date=2019-03-25 19:53:40
//
//        String timeStamp2 = date2TimeStamp(date, "yyyy-MM-dd HH:mm:ss");
//        System.out.println(timeStamp2);  //运行输出:1553514820
//

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        try {
//            Date date = format.parse("2023-01-01");
//            int i = daysBetween(date, new Date());
//            System.out.println(i);
//        }catch (Exception e){
//
//        }

    }
}
